package com.umeox.ota.engine;

import static com.umeox.ota.mvvm.viewModel.MainViewModel.SP_IS_COPY_FILE;

import android.content.Context;
import android.util.Log;

import com.umeox.ota.gloable.App;
import com.umeox.ota.data.constant.Constant;
import com.umeox.ota.data.local.CustomConfig;
import com.umeox.ota.data.local.FotaConfig;
import com.umeox.ota.utils.DeviceUtil;
import com.umeox.ota.utils.Utils;
import com.abupdate.iot_libs.OtaAgentPolicy;
import com.abupdate.iot_libs.data.constant.OtaConstants;
import com.abupdate.iot_libs.data.local.CustomOtaConfig;
import com.abupdate.iot_libs.data.local.OtaConfig;
import com.abupdate.iot_libs.data.local.OtaEntity;
import com.abupdate.iot_libs.engine.DataManager;
import com.abupdate.iot_libs.engine.policy.PolicyConfig;
import com.abupdate.iot_libs.engine.security.FotaException;
import com.abupdate.iot_libs.engine.thread.NamedRunnable;
import com.abupdate.iot_libs.utils.SPFTool;
import com.abupdate.trace.Trace;

/**
 * @author fighter_lee
 * @date 2018/8/30
 * FOTA_SDK初始化任务
 */
public class FotaInitTask extends NamedRunnable {

    private static final String TAG = "FotaInitTask";

    Context context;

    public FotaInitTask(Context context) {
        super(TAG);
        this.context = context;
    }

    @Override
    protected void execute() {
        initTrace();

        OtaAgentPolicy.Builder builder = OtaAgentPolicy.init(context.getApplicationContext())
                .isReportLog(true)
                .setClientDefaultTriggerCycle(CustomConfig.getInstance().fotaCheckCycle);

        String mid = CustomConfig.getInstance().obtainMid();

        //获取到的 mid 是否有效
        try {
            builder.setMid(mid);
            boolean configValid = DeviceUtil.buildConfigValid();
            Log.d(TAG, "execute mid: "+mid+" configValid:"+configValid);
            if (configValid) {
                builder.commit();
            } else {
                //方便调试用
                builder.setMainProjectConfig(
                        new OtaConfig()
                                .setOtaConfig(
                                        new CustomOtaConfig()
                                                .setOem(CustomConfig.getInstance().oem)
                                                .setModels(CustomConfig.getInstance().models)
                                                .setDeviceType(CustomConfig.getInstance().deviceType)
                                                .setPlatform(CustomConfig.getInstance().platform)
                                                .setProductId(CustomConfig.getInstance().productId)
                                                .setProductSecret(CustomConfig.getInstance().productSecret)
                                                .setDownloadFilePath(FotaConfig.getInstance().getDownloadPath()))
                                .setUpdateEngine(new DebugUpdateEngine()))
                        .commit();
            }

            //重置本地升级文件拷贝标志位
            SPFTool.putBoolean(SP_IS_COPY_FILE, false);

            OtaEntity mainEntity = ((App) context.getApplicationContext()).getRepositoryComponent().repositoryManager().obtainDataManager(DataManager.class)
                    .getMainEntity();
            PolicyConfig policyConfig = mainEntity.getPolicyManager().getPolicyConfig();
            policyConfig.request_battery(CustomConfig.getInstance().batteryJudge)
                    .parsePolicyYourself(OtaConstants.PolicyType.TYPE_DOWNLOAD_FORCE, new PolicyForceDownload())
                    .parsePolicyYourself(OtaConstants.PolicyType.TYPE_DOWNLOAD_REQUEST_WIFI, new PolicyDLWifi())
                    .parsePolicyYourself(OtaConstants.PolicyType.TYPE_INSTALL_FORCE, new PolicyInstallForce());
            //设置初始化任务完成
            initCompleted();
        } catch (FotaException e) {
            //若出现异常打印，请及时处理
            Trace.e(TAG, "init fail ", e);
        }
    }

    private void initTrace() {
        Trace.setLevel(Trace.DEBUG);
        Trace.setLog_path(Utils.setFotaLog(context));
        Trace.setShowPosition(true);
    }

    public static void initStart(Context context) {
        SPFTool.init(context);
        SPFTool.putBoolean(Constant.SP_FOTA_INIT_COMPLETED, false);
    }

    public static void initCompleted() {
        SPFTool.putBoolean(Constant.SP_FOTA_INIT_COMPLETED, true);
    }

    public static boolean isCompleted() {
        return SPFTool.getBoolean(Constant.SP_FOTA_INIT_COMPLETED, false);
    }
}
